const filterData = require("./filterData.js");

Component({
    properties: {
      houseData: {
        type: Array,
        value: 'housesList'
      },
      currentTime: {
        type: String,
        value: 'currentTime'
      }
    },
    data: {
        ...filterData,
        contentShow: null,
        isMaskShow: true,
        selectTime:false,
        // selectExpired:false
    },
    
    methods: {
      //获取当前时间
      getDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        month = month < 10 ? '0' + month : month
        var day = date.getDate();
        day = day < 10 ? ('0' + day) : day
        var time = `${year}-${month}-${day}`
        this.setData({ currentTime: time })
        console.log(time)
      },
        // 重置select遍历方法
        resetSelect(data){
            data.forEach(ele => {
                ele.select = false;
            });
            return data;
        },
        expired(e){
          let {expired,selectExpired} = this.data
          if (expired == 0) return
          this.setData({
            expired:expired?1:0
          })
          
          let param = this.creatParam();
          this.subParam(param);
        },
        bindDateChange1(e){
          if(e.detail.value>this.data.endTime&&this.data.endTime!=""){
            wx.showToast({
              title:'开始时间必须小于等于结束时间',
              duration:3000,
              icon:'none'
            })
            return
          }
          this.setData({
            startTime:e.detail.value,
            recommendTimeData: this.resetSelect(this.data.recommendTimeData),
            selectTime:true
          })
          if(this.data.endTime){
            let param = this.creatParam();
            this.subParam(param);
          }
        },
        bindDateChange2(e){
          if(e.detail.value<this.data.startTime&&this.data.startTime!=""){
            wx.showToast({
              title:'结束时间必须大于开始时间',
              duration:3000,
              icon:'none'
            })
            return
          }

          this.setData({
            endTime:e.detail.value,
            recommendTimeData: this.resetSelect(this.data.recommendTimeData),
            selectTime:true
          })
          if(this.data.startTime){
            let param = this.creatParam();
            this.subParam(param);
          }
        },
        // 标题切换交互绑定
        headerSelect(e){
            const index = e.target.dataset.index;
            let headerTitle = this.data.headerTitle;
            let select = headerTitle[index].select;
            if (select) {
                headerTitle.forEach(ele => {
                    ele.select = false;
                });
                this.setData({contentShow: null, isMaskShow: true, headerTitle})
            } else {
                headerTitle.forEach(ele => {
                    ele.select = false;
                });
                headerTitle[index].select = true;
                this.setData({
                    contentShow: index,
                    headerTitle,
                    isMaskShow: false,
                });
            }
        },
        // 楼盘选择交互绑定
        houseSelect(e){
            const index = e.target.dataset.index;
            let houseData = this.resetSelect(this.data.houseData);
            let select = houseData[index].select;
            houseData[index].select = select? false : true;
            this.setData({
                houseData,
            });
            this.triggerEvent('houseChange', houseData);
            this.subBtn()
        },
        // 排序选择交互绑定
        sortSelect(e){
          const index = e.target.dataset.index;
          let timeTypeData = this.resetSelect(this.data.timeTypeData);
          timeTypeData[index].select = true;
          this.setData({timeTypeData});
          let param = this.creatParam();
          this.subParam(param);
        },
        statusSelect(e){
          const index = e.target.dataset.index;
          let statusData = this.resetSelect(this.data.statusData);
          statusData[index].select = true;
          this.setData({statusData});
          let param = this.creatParam();
          this.subParam(param);
        },
        timeSelect(e){
          const index = e.target.dataset.index;
          let recommendTimeData = this.resetSelect(this.data.recommendTimeData);
          recommendTimeData[index].select = true;
          this.setData({recommendTimeData});
          let param = this.creatParam();
          this.subParam(param);
        },

        // 楼盘选项卡清空选项
        houseClear(){
          const that = this;
          let {houseData} = this.data;
          this.setData({
            houseData: that.resetSelect(houseData),
            productId:''
          })
          houseData.forEach(item=>{
            item.select = false
          })
          this.triggerEvent('houseChange', houseData);
          this.subBtn();
        },
        // 排序选项卡清空选项
        sortClear(){
          const that = this;
          let {timeTypeData} = this.data;
          this.setData({
            timeTypeData: that.resetSelect(timeTypeData)
          })
          this.subBtn();
        },
        // 客户状态选项卡清空选项
        statusClear(){
          const that = this;
          let {statusData} = this.data;
          this.setData({
            statusData: that.resetSelect(statusData),
            expired:null
          })
          this.subBtn();
        },
        // 推介时间选项卡清空选项
        recommendTimeClear(){
          const that = this;
          let {recommendTimeData,startTime,endTime} = this.data;
          this.setData({
            recommendTimeData: that.resetSelect(recommendTimeData),
            startTime:"",
            endTime:"",
            selectTime:false
          })
          this.subBtn();
        },
        // 遮罩层隐藏方法
        hideMask(){
            this.setData({
                isMaskShow: true,
                contentShow: null,
                headerTitle: this.resetSelect(this.data.headerTitle),
            })
        },
        
        subParam(param){
            if (JSON.stringify(param) === "{}" || param === "" || param === null || param === undefined) {
                param = this.creatParam();
            }
            this.triggerEvent('myevent', param);
            this.hideMask();
            console.log('param', param);
        },
        subBtn(){
            this.subParam();
        },
        // 创建组装参数方法
        creatParam(){
            let {houseData,
                timeTypeData,
                statusData,
                recommendTimeData,
                headerTitle,
                endTime,
                startTime,
                expired
                } = this.data;
            let productId = "",//楼盘
            timeType = null,//排序
            status = null,//客户状态
            recommendTime = null,//时间
            houseNum = 0,
            timeTypNum = 0,
            statusNum = 0,
            recommendTimeNum = 0;

            // 排序方式
            // debugger
            houseData.forEach(ele => {
                if (ele.select === true) {
                  productId = ele.productId;
                  houseNum = 1;
                }
            })
            timeTypeData.forEach(ele => {
              if (ele.select === true) {
                timeType = ele.key;
                timeTypNum = 1;
              }
            })
            statusData.forEach(ele => {
              if (ele.select === true) {
                status = ele.key;
                statusNum = 1;
              }
            })
            recommendTimeData.forEach(ele => {
              if (ele.select === true) {
                recommendTime = ele.key;
                recommendTimeNum = 1;
              }
            })
            //debugger
            headerTitle[0].num = houseNum;
            headerTitle[1].num = timeTypNum;
            headerTitle[2].num = statusNum;
            headerTitle[3].num = recommendTimeNum;
            this.setData({
                headerTitle
            })

            return {
                productId,
                timeType,
                status,
                recommendTime,
                endTime,
                startTime,
                expired
            };
        }
    },
  lifetimes: {
    created() {
      this.getDate()
    }
  }
  })